// 引入async模块
let async = require('async');
// 引入常量模块
let Constant = require('../constant/constant');
// 定义一个对象
let exportObj = {
	clone,
	checkUrl,
	changeNumber,
	js_date_time,
	isEmpty,
	pad,
	getDay,
	compare,
	transfromDataList,
	getFileSize,
	timestampToDate,
	getIds
};

function getIds(source, type) {
	let ids = []
	switch (type) {
		case "longSmall":
			ids = ["adunit-e85248ccd8a0fd46"]
			break;
		case "pptDetail":
			ids = ["adunit-0b074e62081c35ca"]
			break;
		case "jlsp":
			ids = "adunit-5db7f871fef036ca"
			break;
		default:
			ids = []
			break;
	}
	return ids
}

function timestampToDate(timestamp) {
	let date = new Date(timestamp * 1000);
	let year = date.getFullYear();
	let month = date.getMonth() + 1; // 月份从0开始，需要加1
	let day = date.getDate();
	return `${year}-${month}-${day}`;
}
// 导出对象，方便其他方法调用
module.exports = exportObj;

/**
 * 数组对象排序
 * @param obj
 * @returns {any}
 */
function compare(attr, rev) {
	if (rev == undefined) {
		rev = 1;
	} else {
		rev = (rev) ? 1 : -1;
	}

	return function (a, b) {
		a = a[attr];
		b = b[attr];
		if (a < b) {
			return rev * -1;
		}
		if (a > b) {
			return rev * 1;
		}
		return 0;
	}
}

/**
 * 克隆方法，生成一个默认成功的返回
 * @param obj
 * @returns {any}
 */
function clone(obj) {
	return JSON.parse(JSON.stringify(obj));
}

//判断变量是否为空
function isEmpty(param) {
	var param_type = typeof (param);
	if (param_type == 'undefined') {
		return true;//非空值
	} else {
		if (param_type == 'object') {
			//要判断的是【对象】或【数组】或【null】等
			if (typeof (param.length) == 'undefined') {
				if (JSON.stringify(param) == "{}") {
					return true;//空值，空对象
				}
			} else if (param.length == 0) {
				return true;//空值，空数组
			}
		} else if (param_type == 'string') {
			//如果要过滤空格等字符
			var new_param = param.trim();
			if (new_param.length == 0) {
				//空值，例如:带有空格的字符串" "。
				return true;
			}
		} else if (param_type == 'boolean') {
			return false;//非空值
		} else if (param_type == 'number') {
			if (param >= 0) {
				return false;
			} else {
				return true;
			}
		}
		return false;//非空值
	}
}

/**
 * 校验参数全局方法
 * @param params   请求的参数集
 * @param checkArr 需要验证的参数
 * @param cb       回调
 */
function checkUrl(params, checkArr, response) {
	//console.log(params);
	let flag = true;

	if (checkArr && checkArr.length > 0) {
		checkArr.forEach(v => {
			if (!params[v]) {
				flag = false;
			}
		});
	}

	if (flag == false) {
		response.json(Constant.LACK);
		return false
	} else {
		return true
	}
}

function changeNumber(value) {
	if (typeof value == "string") {
		value = Number(Number(value).toFixed(3))
	}
	if (typeof value == "number") {
		value = Number(value.toFixed(3))
	}

	if (typeof value == "object") {
		for (let key1 in value) {
			if (typeof value[key1] == "number") {
				value[key1] = Number(value[key1].toFixed(3))
			}

			if (typeof value[key1] == "object") {
				for (let key2 in value[key1]) {
					if (typeof value[key1][key2] == "number") {
						value[key1][key2] = Number(value[key1][key2].toFixed(3))
					}
				}
			}
		}
	}

	return value
}

function js_date_time(unixtime, is_full = true) {
	let date = new Date(Number(unixtime));
	let y = date.getFullYear();
	let m = date.getMonth() + 1;
	m = m < 10 ? ('0' + m) : m;
	let d = date.getDate();
	d = d < 10 ? ('0' + d) : d;
	let h = date.getHours();
	h = h < 10 ? ('0' + h) : h;
	let minute = date.getMinutes();
	let second = date.getSeconds();
	minute = minute < 10 ? ('0' + minute) : minute;
	second = second < 10 ? ('0' + second) : second;
	// return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;//年月日时分秒
	if (is_full) {
		return y + '-' + m + '-' + d + ' ' + h + ':' + minute;
	} else {
		return y + '-' + m + '-' + d;
	}
}

//获取时间
function getDay(day) {
	let today = new Date();
	let targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
	today.setTime(targetday_milliseconds);
	let tYear = today.getFullYear();
	let tMonth = today.getMonth() + 1 < 10 ? '0' + (today.getMonth() + 1) : today.getMonth() + 1;
	let tDate = today.getDate() < 10 ? '0' + today.getDate() : today.getDate();

	return tYear + "-" + tMonth + "-" + tDate;
}

//偏移数字带几个0
function pad(num, n) {
	var len = num.toString().length;
	while (len < n) {
		num = "0" + num;
		len++;
	}
	return num;
}

function transfromDataList(list = []) {
	let newList = []
	for (let item of list) {
		item.backgroundImage = ''
		if (item.artPPT) {
			item = Object.assign(item, item.artPPT)
			delete item.artPPT
		}

		if (item.images && item.images.length > 0) {
			for (let thisImage of item.images) {
				thisImage = thisImage.split("?imageMogr2/format/WebP")[0]
				if (item.isNew) {
					item.backgroundImage = thisImage + "/small"
				}
				if (thisImage.indexOf("/sf") == -1) thisImage = thisImage + "/sf"
			}
		} else {
			if (item.fileUrls && item.fileUrls.length > 0) {
				if (item.fileType == "课件") {
					item.images[0] = item.fileUrls[0].fileUrl + '?ci-process=doc-preview&dstType=png&scale=20'
				} else if (item.fileType == "素材") {
					item.images[0] = item.fileUrls[0].fileUrl + '/sf'
				}
			}
		}

		if (item.fileUrls && item.fileUrls.length > 0) {
			if (item.typeDesc && item.typeDesc.name) item.typeDesc = item.typeDesc.name;
			item.fileUrl = item.fileUrls[0].fileUrl
			item.size = item.fileUrls[0].size
			delete item.fileUrls
			newList.push(item)
		}

		delete item.baiduDeskLink
	}
	return list
}

function getFileSize(limit) {
	let size = (limit / (1024 * 1024)).toFixed(2) + "MB"

	var sizeStr = size + ""; //转成字符串
	var index = sizeStr.indexOf("."); //获取小数点处的索引
	var dou = sizeStr.substr(index + 1, 2) //获取小数点后两位的值
	if (dou == "00") { //判断后两位是否为00，如果是则删除00                
		return sizeStr.substring(0, index) + sizeStr.substr(index + 3, 2)
	}
	return size;
}
